package local.sumw3.findmydevices;

import local.sumw3.findmydeivces.R;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;

public class MsgSendFragment extends DialogFragment {

    static MsgSendFragment newInstance() {
        MsgSendFragment f = new MsgSendFragment();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_msgsend, container, false);
        final EditText mEtTitle = (EditText) v.findViewById(R.id.etMsgSendTitle);
        final EditText mEtContent = (EditText) v.findViewById(R.id.etMsgSendContent);
        v.findViewById(R.id.btnMsgSendCancel).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MsgSendFragment f = (MsgSendFragment) getFragmentManager().findFragmentByTag(
                        UIConstants.FRAGMENT_TAG_MAINACTIVITY);
                if (f != null) {
                    f.dismiss();
                }
            }
        });

        v.findViewById(R.id.btnMsgSendOK).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mEtContent.getText() == null || "".equals(mEtContent.getText().toString())) {
                    mEtContent.setError(getString(R.string.inputerror_msgsend_content));
                    return;
                }
                String content = mEtContent.getText().toString();
                String title = "";
                if (mEtTitle.getText() != null) {
                    title = mEtTitle.getText().toString();
                }
                ((MainActivity) getActivity()).sendMsg(title, content);

                MsgSendFragment f = (MsgSendFragment) getFragmentManager().findFragmentByTag(
                        UIConstants.FRAGMENT_TAG_MAINACTIVITY);
                if (f != null) {
                    f.dismiss();
                }
            }
        });
        return v;
    }
}
